package com.iknow.xmpp.smack.avatar;

import com.iknow.xmpp.smack.pep.PEPListener;
import com.iknow.xmpp.smack.pep.PepSubManager;
import java.io.IOException;
import java.io.PrintStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;

public class AvatarManager
{
  public static final String AVATARDATA_NODE = "urn:xmpp:avatar:data";
  public static final String AVATARMETADATA_NODE = "urn:xmpp:avatar:metadata";
  private boolean mAutoDownload;
  private AvatarCache mCache;
  private Connection mCon;
  private final List<AvatarListener> mListeners = new LinkedList();
  private PepSubManager mPep;

  public AvatarManager(Connection paramConnection, PepSubManager paramPepSubManager, AvatarCache paramAvatarCache, boolean paramBoolean)
  {
    this.mCon = paramConnection;
    this.mPep = paramPepSubManager;
    this.mAutoDownload = paramBoolean;
    this.mCache = paramAvatarCache;
    this.mPep.addPEPListener(new Listener(this));
  }

  protected AvatarManager(Connection paramConnection, PepSubManager paramPepSubManager, boolean paramBoolean)
  {
    this.mCon = paramConnection;
    this.mPep = paramPepSubManager;
    this.mAutoDownload = paramBoolean;
    this.mPep.addPEPListener(new Listener(this));
  }

  private void fireListeners(String paramString1, String paramString2, List<AvatarMetadataExtension.Info> paramList)
  {
    Iterator localIterator = this.mListeners.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      ((AvatarListener)localIterator.next()).onAvatarChange(paramString1, paramString2, paramList);
    }
  }

  private void publishAvatarData(String paramString, byte[] paramArrayOfByte)
  {
    PayloadItem localPayloadItem = new PayloadItem(paramString, new AvatarExtension(paramArrayOfByte));
    this.mPep.getPEPNode("urn:xmpp:avatar:data").publish(localPayloadItem);
  }

  public void addAvatarListener(AvatarListener paramAvatarListener)
  {
    if (!(this.mListeners.contains(paramAvatarListener)))
      this.mListeners.add(paramAvatarListener);
  }

  public void disableAvatarPublishing()
  {
    publishAvatarMetaData(null, new AvatarMetadataExtension());
  }

  public boolean downloadAvatar(String paramString1, String paramString2, AvatarMetadataExtension.Info paramInfo)
  {
    int i;
    byte[] arrayOfByte;
    try
    {
      arrayOfByte = AvatarRetrieverFactory.getRetriever(this.mCon, paramString1, paramInfo).getAvatar();
      this.mCache.put(paramString2, arrayOfByte);
      i = 1;
      return i;
    }
    catch (IOException localIOException)
    {
      System.err.println("Error while downloading avatar");
      localIOException.printStackTrace();
      i = 0;
    }
  }

  public byte[] getAvatar(String paramString)
  {
    Object localObject;
    if (paramString == null)
    {
      localObject = null;
      return localObject;
    }
    try
    {
      byte[] arrayOfByte = this.mCache.get(paramString);
      localObject = arrayOfByte;
    }
    catch (IOException localIOException)
    {
      localObject = null;
    }
  }

  protected String getAvatarId(byte[] paramArrayOfByte)
    throws NoSuchAlgorithmException
  {
    return StringUtils.encodeHex(MessageDigest.getInstance("sha-1").digest(paramArrayOfByte));
  }

  public boolean publishAvatarData(byte[] paramArrayOfByte)
  {
    int i;
    try
    {
      publishAvatarData(getAvatarId(paramArrayOfByte), paramArrayOfByte);
      i = 1;
      return i;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      System.err.println("Security error while publishing avatar data : " + localNoSuchAlgorithmException.getMessage());
      i = 0;
    }
  }

  public void publishAvatarMetaData(String paramString, AvatarMetadataExtension paramAvatarMetadataExtension)
  {
    PayloadItem localPayloadItem = new PayloadItem(paramString, paramAvatarMetadataExtension);
    this.mPep.getPEPNode("urn:xmpp:avatar:metadata").publish(localPayloadItem);
  }

  public void removeAvatarListener(AvatarListener paramAvatarListener)
  {
    this.mListeners.remove(paramAvatarListener);
  }

  protected AvatarMetadataExtension.Info selectAvatar(List<AvatarMetadataExtension.Info> paramList)
  {
    return ((AvatarMetadataExtension.Info)paramList.get(0));
  }

  private class Listener
  implements PEPListener
  {
    public void eventReceived(, String paramString2, List<Item> paramList)
    {
      if (!("urn:xmpp:avatar:metadata".equals(paramString2)));
      while (true)
      {
        Item localItem;
        PacketExtension localPacketExtension;
        do
        {
          do
          {
            return;
            localItem = (Item)paramList.get(0);
          }
          while (!(localItem instanceof PayloadItem));
          localPacketExtension = ((PayloadItem)localItem).getPayload();
        }
        while (!(localPacketExtension instanceof AvatarMetadataExtension));
        AvatarMetadataExtension localAvatarMetadataExtension = (AvatarMetadataExtension)localPacketExtension;
        String str = localItem.getId();
        List localList = localAvatarMetadataExtension.getInfos();
        if ((localList.size() > 0) && (AvatarManager.access$0(this.this$0)))
        {
          AvatarMetadataExtension.Info localInfo = this.this$0.selectAvatar(localList);
          if (!(AvatarManager.access$1(this.this$0).contains(str)))
            this.this$0.downloadAvatar(paramString1, str, localInfo);
        }
        AvatarManager.access$2(this.this$0, paramString1, str, localList);
      }
    }
  }
}